//
//  HMUserAccount.swift
//  Weibo-walker
//
//  Created by 郭彬 on 16/3/9.
//  Copyright © 2016年 guobin. All rights reserved.
//

import UIKit

class HMUserAccount: NSObject {
    
    
//定义数据信息
    //用户在新浪微博的 id
    var uid:String?
    //登录的过期时间
    /// 过期时间,单位是 `秒`
    var expires_in: NSTimeInterval = 0 {
        didSet{
            // 过期时间等于 获取回来accessToken的那一刻加上 `expires_in` 这么多秒
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }
    //过期时间，data 类型
    var expiresDate: NSDate?

    /// access_token
    var access_token: String?
    /// 过期时间的字符串类型
    var remind_in: String?
    /// 用户的大图 180*180
    var avatar_large: String?
    /// 用户的昵称
    var screen_name: String?
    
    
    init(dict:[String: AnyObject]){
        super.init()

        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        let keys = ["uid", "expires_in", "access_token", "avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    //归档
    func encodeWithCoder(encoder: NSCoder) {
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(screen_name, forKey: "screen_name")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")
    }

    
    //解档
    required init(encoder decoder:NSCoder) {
        uid = decoder.decodeObjectForKey("uid") as? String
        access_token = decoder.decodeObjectForKey("access_token") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        screen_name = decoder.decodeObjectForKey("screen_name") as? String
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
    }
    
}
